package com.hpe.aruba.CPPM.utils;

import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MacAdressMatcher {


    private static final String TAG = "MAC Adress Matcher";

    public boolean isMac(String text, boolean isBarcode) {
        text = text.replace("O", "0");
        Pattern smPattern = Pattern.compile("[a-f]+");
        String chars = "A-F";
        if (smPattern.matcher(text).find()) {
            chars = "a-f";
        }
        String delim = "\\.";
        if (text.contains(":")) {
            delim = ":";
        } else if (text.contains("-")) {
            delim = "-";
        }
        String pat = String.format("^(([{0}0-9]'{'2'}'{1})'{'5'}'[{0}0-9]'{'2'}')|(([{0}0-9]'{'3'}'{1})'{'3'}'[{0}0-9]'{'3'}')%s$", isBarcode ? "" : "|([{0}0-9]'{'12'}')");
        String pattern = MessageFormat.format(pat, chars, delim);
        return Pattern.matches(pattern, text);
    }

    public String trimMac(String text) {
        if (text.toUpperCase().startsWith("MAC:")){
            text = text.substring(4);
        }
        text = text.replaceAll("[\\.:]", "-");
        return text;
    }
}
